var results_array = [];
var progress_bar = function(wd,ht,txt,cl) {
	var throwaway = '<img src="/images/progress.gif" id="progress_bar" width="'+wd+'" height="'+ht+'" alt="'+txt+'" title="'+txt+'" align="absmiddle" style="padding:3px;"/>';	
	return throwaway;
};
var loading_bar = function(x,ele) {
	(x) ? $("#"+ele).html(progress_bar(16,16,'','blue')) : $("#"+ele).html('');
};

function delete_cart(id, report_id) {
   $.ajax({
        type: 'GET',
        url: '/api/ajax_view_report.cfm?action=delete_poc&id='+id+'&report_id='+report_id,
        dataType: "json",
        success: function(json) {
            get_report();
        },
        error: function(json) {                
        }                
  }); 
}

var get_report = function() {
	$('#view_pdf').after(''+progress_bar(16,16,'','')+'');
	var local_array = [];
	$.ajax({
        type: 'GET',
        url: '/api/ajax_view_report.cfm?action=get_report&id='+$('#select_report').val(),
        dataType: "json",
        success: function(json) {
            local_array = json.reports;          
        },
        complete: function(json) {
            var result_text = '';
            
            $('#shopping_cart').html('<table width="100%" id="shopping_table" cellspacing="0" cellpadding="0" border="0"><table>');	
            
            $.map(local_array,function(n) {
                $('#shopping_cart').append('<tr><td><a target="_blank" href="/edit.cfm?id=' + n.poc_id + '">' +  n.first_name + ' ' +  n.last_name + '</a>&nbsp;<img src="/images/image_delete.gif" border="0" alt="Delete From Report" title="Delete From Report" align="absmiddle" style=" cursor:pointer;" id="_report_' + n.poc_id  + '" /></td></tr>');
            });           
            
            $("img[id^='_report_']").bind("click", function(e){
                delete_cart(e.target.id.replace('_report_', ""),$('#select_report').val());
             });
              
            $('#progress_bar').remove();
        },
        error: function(json) {                
        }                
   });
   
}

$(function() {
	Functional.install();
	
	$("#report_view_00").bind('click', function(e) {
        get_report();
    });
    $('#select_all').click(function() { 
        var checked_status = this.checked;
        $("input[id^='delete_']").each(function()
        {
            this.checked = checked_status;
        });							
	});
    $("#view_pdf").bind('click', function(e) {
        window.open('/pdf_report.cfm?id='+$('#select_report').val(),'NIPD','');
    });

	
});